"""
逻辑接口层
    用户接口
"""
from db import db_handler
from lib import common
#产生一个日志对象
logginger = common.get_log("ures")
#注册接口
def register_interface(user_name,password,balance=15000):
    # (3)查看用户是否存在
    user_dic = db_handler.select(user_name)
    if user_dic:
        return False,f"{user_name}已经注册！"
    #（4）若用户不存在，则保存用户数据
    password = common.get_password_sha3_512(password)
    user_dic={
            "user_name":user_name,
            "password": password,
            "balance":balance,
            #流水记录
            "flow":[],
            #购物车
            "shop_car":{},
            #冻结状态
            "locked":False
            }
    db_handler.save(user_dic)
    msg = f"{user_name}注册成功！"
    logginger.info(msg)
    return True,msg

#登录接口
def login_interface(user_name,password):
    user_dic=db_handler.select(user_name)
    if user_dic["locked"] ==True:
        return False,f"{user_name}用户已被冻结！"
    password = common.get_password_sha3_512(password)
    if user_dic:
        if password ==user_dic.get("password"):
            msg = f"{user_name} 登陆成功！"
            logginger.info(msg)
            return True,msg
        else:
            msg = f"{user_name} 密码错误,重新登录！"
            logginger.error(msg)           
            return False,msg
    return False,f"{user_name} 未注册，请去注册！"






